﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HardMobCommons;
using TopicInfoWeb.Models;
using HardMobInfra;
using HardMobCommons.Entidades;

namespace TopicInfoWeb.Controllers
{ 
    public class TopicoController : Controller
    {
		private UnitOfWork db = new UnitOfWork();

        // GET: /Topico/
        public ViewResult Index()
        {
            return View(db.Topicos.Recuperar().ToList());
        }

        // GET: /Topico/Details/5
        public ViewResult Details(int id)
        {
            Topico topico = db.Topicos.Recuperar(id);
            return View(topico);
        }

        // GET: /Topico/Create
        public ActionResult Create()
        {
            return View();
        } 

        // POST: /Topico/Create
        [HttpPost]
        public ActionResult Create(Topico topico)
        {
            if (ModelState.IsValid)
            {
                db.Topicos.Inserir(topico);
                db.SalvarAlteracoes();
                return RedirectToAction("Index");  
            }

            return View(topico);
        }
        
        //
        // GET: /Topico/Edit/5
 
        public ActionResult Edit(int id)
        {
            Topico topico = db.Topicos.Recuperar(id);
            return View(topico);
        }

        // POST: /Topico/Edit/5
        [HttpPost]
        public ActionResult Edit(Topico topico)
        {
            if (ModelState.IsValid)
            {
				db.Topicos.Atualizar(topico);
				db.SalvarAlteracoes();

                return RedirectToAction("Index");
            }
            return View(topico);
        }

        // GET: /Topico/Delete/5
        public ActionResult Delete(int id)
        {
            Topico topico = db.Topicos.Recuperar(id);
            return View(topico);
        }

        //
        // POST: /Topico/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            db.Topicos.Delete(id);
			db.SalvarAlteracoes();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}